package com.example.goroha.calendardatabase;

import java.io.IOException;
import java.io.InputStream;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class CalendarDbHelper extends SQLiteOpenHelper {
    private static final String TAG = "CalendarDbHelper";
    private static final boolean D = true;

    // Define the version and database file name
    private static final String DB_NAME = "calendar.db";
    private static final int DB_VERSION = 1;

    /** Use a static class to defined the data structure
    * This will come in very handy if you using Agile
    * As your development model
    **/
    public static class UserTable {
        public static final String NAME = "_Calendar";
        public static final String COL_ID = "_id";
        public static final String COL_NAME = "_name";
        public static final String COL_CONTENT = "_content";
        //public static final String COL_PASSWORD = "password";
    }

    private SQLiteDatabase db;
    private Context myContext;

    public CalendarDbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        // Android will look for the database defined by DB_NAME
        // And if not found will invoke your onCreate method
        db = getWritableDatabase();
        this.myContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        if(D) Log.d(TAG, "onCreate");
        String CREATE_TABLE = "CREATE TABLE " + UserTable.NAME +
                " ( " + UserTable.COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT , " +

                UserTable.COL_NAME + " TEXT , " +
                UserTable.COL_CONTENT + " TEXT ); ";
        if(D) Log.d(TAG, CREATE_TABLE);
        //database =getWritableDatabase();
        database.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        if(D) Log.d(TAG, "onUpgrade");
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + UserTable.NAME);

        // Create tables again
        onCreate(db);

    }

    public void DataProcessing() {
		/* Get input path */
        AssetManager assetManager = myContext.getAssets();

        try {
            String files[] = assetManager.list("database");
            for(int i = 0; i < files.length; i++) {
                if(D) Log.d(TAG, files[i]);

                // To load text file
                InputStream input;
                input = assetManager.open("database/" + files[i]);

                int size = input.available();
                byte[] buffer = new byte[size];
                input.read(buffer);
                input.close();

                // byte buffer into a string
                String text = new String(buffer);
                if(D) Log.d(TAG, "Try to insert new data ...");
                insertNewData(files[i], text);
                if(D) Log.d(TAG, "Inserted new data ...");
                // if(D) Log.d(TAG, text);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return;
//		/* Create new Output folder */
//		File outputPath = new File(inputPath.getParent() + "\\Output");
//		outputPath.delete();
//		outputPath.mkdir();
//
//		/* Get all files in input path */
//		File listOfFiles[] = inputPath.listFiles();
//		if(listOfFiles.length == 0) {
//			System.out.println("Input path is empty folder");
//			System.exit(1);
//		}
//
//		/* Process for single file */
//		for(int i = 0; i < listOfFiles.length; i++) {
//			//System.out.println(listOfFiles[i].getPath());
//			DataPreparation.DataProcessing(listOfFiles[i], outputPath);
//			//PureDataProcessing(listOfFiles[i], outputPath);
//		}
    }

    private void insertNewData(String name, String content) {
        String insertName = "\"" + name + "\"";
        String insertContent = "\"" + content + "\"";

        String INSERT_INTO_TABLE = "INSERT INTO " + UserTable.NAME +
                " ( " + UserTable.COL_NAME + " , " + UserTable.COL_CONTENT + " ) " +
                " VALUES ( " + insertName + " , " + insertContent + " ); ";

        if(D) Log.d(TAG, INSERT_INTO_TABLE);
        //db = this.getWritableDatabase();
        //db.execSQL(INSERT_INTO_TABLE);
        ContentValues contentValues = new ContentValues();
        contentValues.put(UserTable.COL_NAME, name);
        contentValues.put(UserTable.COL_CONTENT, content);
        db.insert(UserTable.NAME, null, contentValues);
    }
}
